﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using UTSDAL.LINQSQL.Model;
using UTSDAL.LINQSQL.DAO;
using UTSDAL.LINQSQL.Helper;
using UTSDAL.SQL;
using log4net;
using System.Data.Linq.Mapping;
using System.Data.SqlClient;

public partial class List : System.Web.UI.Page {
    protected System.Web.DynamicData.MetaTable table;
        
    protected void Page_Init(object sender, EventArgs e) {
        DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
    }

    
    protected void Page_Load(object sender, EventArgs e) {
        table = GridDataSource.GetTable();
        Title = table.DisplayName;
        
        
        InsertHyperLink.NavigateUrl = table.GetActionPath(PageAction.Insert);

        // Disable various options if the table is readonly 
        if (table.IsReadOnly ) {
            GridView1.Columns[0].Visible = false;
            InsertHyperLink.Visible = false;
        }
        //    we dont want to add any further days to the calendar as there are only 7 possible daysw
        else if (table.Name.Equals("CourseDays"))
        {
            
            InsertHyperLink.Visible = false;
            DeleteAll.Visible = false;
        }

        if (Session["InsertPerformed"] != null)
        {
            Message.ImagePath = "~/images/tick.png";
            Message.Msg = "Record has successfully been added to "+table.DisplayName;
            Session.Remove("InsertPerformed");
        }

        else if (Session["UpdatePerformed"] != null)
        {
            Message.ImagePath = "~/images/tick.png";
            Message.Msg = "Record has successfully been updated in " + table.DisplayName;
            Session.Remove("UpdatePerformed");
        }
        else if(Session["DeleteFailed"]!=null)
        {

            Message.ImagePath = "~/images/cross.png";
            Message.Msg = "Could not delete this record as it is still being referenced elsewhere ";
            Session.Remove("DeleteFailed");
        }
        else if (Session["DeleteAllComplete"] != null)
        {
            Message.ImagePath = "~/images/tick.png";
            Message.Msg = "All records deleted ";
            Session.Remove("DeleteAllComplete");
        }
        else if (Session["DeleteAllFailed"] != null)
        {
            Message.ImagePath = "~/images/cross.png";
            Message.Msg = "Could not delete all records as they are still being referenced in other tables ";
            Session.Remove("DeleteAllFailed");
        }




    }

    protected void OnFilterSelectedIndexChanged(object sender, EventArgs e) {
        GridView1.PageIndex = 0;
    }

    protected void GridDataSource_Deleted(object sender, LinqDataSourceStatusEventArgs e)
    {
        if (e.Exception == null || e.ExceptionHandled)
        {
            Message.Visible = true;
            Message.ImagePath = "~/images/tick.png";
            Message.Msg = "Record has successfully been deleted ";
        }
        else
        {
            Message.Visible = true;
            Session.Add("DeleteFailed", "DeleteFailed");
            Response.Redirect(table.ListActionPath);
        
        }

    }

    private static readonly ILog logger = LogManager.GetLogger(typeof(List));

    protected void DeleteAllItems(object sender, EventArgs e)
    {

        logger.Debug("Delete All " + table.EntityType.Name);

        Exception deleteException = null;

        try
        {

            String query = "DELETE FROM " + table.EntityType.Name;
            ConnectionManager connectionManager = ConnectionManager.getInstance();
            connectionManager.update(query, null);

            logger.Debug("Completed Delete All " + table.EntityType.Name);
        }
        catch (SqlException exception)
        {
            deleteException = exception;
        }


        if (deleteException != null)
        {
            Session.Add("DeleteAllFailed", "DeleteAll");
        }
        else
        {
            Session.Add("DeleteAllComplete", "DeleteAll");
        }

        Response.Redirect(table.ListActionPath);
        
        
    }

    protected void DeleteLinkButton_PreRender(object sender, EventArgs e)
    {
        LinkButton deleteButton = (LinkButton)sender;
        if (table.Name.Equals("CourseDays"))
        {
            deleteButton.Visible = false;
        }
    }

    protected void EditHyperLink_PreRender(object sender, EventArgs e)
    {
        logger.Debug("EditHyperLink -> " + table.Name);

        HyperLink editButton = (HyperLink)sender;

        String[] unEditableTables = new String[] { "ModuleEventFacilities", "DepartmentRooms", "RoomFacilities","CohortModules" };
        
        if (unEditableTables.Contains(table.Name))
        {
            editButton.Visible = false;
        }

    }
}
